﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel;
using SGCG.Helpers;

namespace SGCG.Model
{
    public class TemporaryGroupingOfBorderedObjects : NotifyPropertyChanged, IHasBorder, INotifyPropertyChanged, IFillable, IHasArrow
    {
        private readonly IEnumerable<IHasBorder> sourceCollection;

        public TemporaryGroupingOfBorderedObjects(IEnumerable<GraphicObject> sourceCollection)
        {
            this.sourceCollection = sourceCollection.OfType<IHasBorder>();
        }

        public LineStyle LineStyle
        {
            get
            {
                try
                {
                    return this.sourceCollection.Select(o => o.LineStyle).Distinct().Single();
                }
                catch
                {
                    return LineStyle.Normal;
                }
            }
            set
            {
                this.sourceCollection.Each(o => o.LineStyle = value);
                RaisePropertyChanged("LineStyle");
            }
        }

        public LineThickness LineThickness
        {
            get
            {
                try
                {
                    return this.sourceCollection.Select(o => o.LineThickness).Distinct().Single();
                }
                catch
                {
                    return LineThickness.Normal;
                }
            }
            set
            {
                this.sourceCollection.Each(o => o.LineThickness = value);
                RaisePropertyChanged("LineThickness");
            }
        }

        public Color Color
        {
            get
            {
                try
                {
                    return this.sourceCollection.Select(o => o.Color).Distinct().Single();
                }
                catch
                {
                    return Colors.Black;
                }
            }
            set
            {
                this.sourceCollection.Each(o => o.Color = value);
                RaisePropertyChanged("Color");
            }
        }

        public FillType Fill
        {
            get
            {
                try
                {
                    return this.sourceCollection.OfType<IFillable>().Select(o => o.Fill).Distinct().Single();
                }
                catch
                {
                    return FillType.None;
                }
            }
            set
            {
                this.sourceCollection.OfType<IFillable>().Each(o => o.Fill = value);
                RaisePropertyChanged("Fill");
            }
        }

        public Color FillColor
        {
            get
            {
                try
                {
                    return this.sourceCollection.OfType<IFillable>().Select(o => o.FillColor).Distinct().Single();
                }
                catch
                {
                    return Colors.Black;
                }
            }
            set
            {
                this.sourceCollection.OfType<IFillable>().Each(o => o.FillColor = value);
                RaisePropertyChanged("FillColor");
            }
        }

        public ArrowType ArrowType
        {
            get
            {
                try
                {
                    return this.sourceCollection.OfType<IHasArrow>().Select(o => o.ArrowType).Distinct().Single();
                }
                catch
                {
                    return ArrowType.Nenhum;
                }
            }
            set
            {
                this.sourceCollection.OfType<IHasArrow>().Each(o => o.ArrowType = value);
                RaisePropertyChanged("ArrowType");
            }
        }
    }
}
